import 'package:flutter/material.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:get/get.dart';
import 'package:smart_lam/widget/flutter_circle_color_picker.dart';

class LampController extends GetxController {
  late CircleColorPickerController? colorController;
  late DiscoveredDevice d;

  @override
  void onInit() {
    d = Get.arguments as DiscoveredDevice;
    colorController = CircleColorPickerController(initialColor: Color(0xFF123456));
    super.onInit();
  }

  @override
  void onReady() async {
    super.onReady();
  }

  void onChanged(Color v) async{
    colorController!.color = v;
    update();
    try {
      var ble = FlutterReactiveBle();
      print(d.serviceUuids.first);
      final characteristic = QualifiedCharacteristic(serviceId: d.serviceUuids.first, characteristicId: Uuid([255, 1]), deviceId: d.id); 
      await ble.writeCharacteristicWithResponse(characteristic, value: [0, 254]);
      final characteristic1 = QualifiedCharacteristic(serviceId: d.serviceUuids.first, characteristicId: Uuid([255, 1]), deviceId: d.id);
      final response = await ble.readCharacteristic(characteristic1);
      print(response);
    } catch (e) {
      print(e);
    }
  }
}
